<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="20" :xs="24">
        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="用户id" align="center" key="id" prop="id" v-if="columns[0].visible" />
          <el-table-column label="加密盐" align="center" key="salt" prop="salt" v-if="columns[1].visible"
            :show-overflow-tooltip="true" />
          <el-table-column label="用户名称" align="center" key="name" prop="name" v-if="columns[2].visible"
            :show-overflow-tooltip="true" />
          <el-table-column label="手机号码" align="center" key="phone" prop="phone" v-if="columns[3].visible"
            :show-overflow-tooltip="true" />
          <el-table-column label="性别" align="center" key="sex" prop="sex" v-if="columns[4].visible" >
            <template slot-scope="scope">
              <span v-if="scope.row.sex ">女</span>
              <span v-else>男</span>
            </template>
          </el-table-column>

          <el-table-column label="认证" align="center" key="certification" v-if="columns[5].visible">
            <template slot-scope="scope">
              <span v-if="scope.row.certification ">已认证</span>
              <span v-else>未认证</span>
            </template></el-table-column>
          <el-table-column label="身份认证" align="center" key="identityAuthentication"
            v-if="columns[6].visible">
            <template slot-scope="scope">
              <span v-if="scope.row.identityAuthentication ">已认证</span>
              <span v-else>未认证</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" key="status" v-if="columns[7].visible">
            <template slot-scope="scope">
              <el-switch v-model="!scope.row.status"
                @change="handleStatusChange(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['datemanage:usermanage:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['datemanage:usermanage:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户id" prop="id">
              <el-input v-model="form.id" placeholder="请输入用户昵称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入用户名称" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="email">
              <el-input v-model="form.phone" placeholder="请输入用户手机号" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="认证">
              <el-radio-group v-model="form.certification">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                  :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="身份认证">
              <el-radio-group v-model="form.identityAuthentication">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                  :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                  :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, delUser,updateUser,changeUserStatus,getUser} from "@/api/datamanage/user";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "User",
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `用户id`, visible: true },
        { key: 1, label: `加密盐`, visible: true },
        { key: 2, label: `用户名称`, visible: true },
        { key: 3, label: `手机号码`, visible: true },
        { key: 4, label: `性别`, visible: true },
        { key: 5, label: `认证`, visible: true },
        { key: 6, label: `身份认证`, visible: true },
        { key: 6, label: `状态`, visible: true }
      ],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  watch: {
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams.pageNum,this.queryParams.pageSize).then(response => {
        this.userList = response.data.records;
        this.loading = false;
      }
      );
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === true ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"用户吗？').then(function () {
        return changeUserStatus(row.id);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        row.status = row.status === "0" ? "1" : "0";
      }).catch(function () {
        this.$modal.msgSuccess("已取消更改");
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        salt: undefined,
        name: undefined,
        phone: undefined,
        sex: undefined,
        certification: undefined,
        identityAuthentication: undefined,
        status: "0",
      };
      this.resetForm("form");
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
        this.form.password = "";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + id + '"的数据项？').then(function () {
        return delUser(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>